package com.ruoyi.web.controller.wx;

import cn.hutool.core.lang.tree.Tree;
import com.ruoyi.common.core.controller.BaseController;
import com.ruoyi.common.core.domain.AjaxResult;
import com.ruoyi.framework.web.service.QywxDeptService;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.security.access.prepost.PreAuthorize;
import org.springframework.web.bind.annotation.*;

import java.util.List;

/**
 * 企业微信部门
 *
 * @author zhousiren
 * @since 2025/3/3 11:21
 */
@RestController
@RequestMapping("/qyweixin/dept")
public class QywxDeptController extends BaseController {

    @Autowired
    private QywxDeptService qywxDeptService;

    //--------------------------- 企业微信 相关 ---------------------------

    /**
     * 获取所有部门
     *
     * @param corpid
     * @return
     */
    @PreAuthorize("@ss.hasPermi('system:dept:list')")
    @GetMapping("/deptTree")
    public AjaxResult deptTree(@RequestParam(value = "corpid", required = false) String corpid) {
        List<Tree<Integer>> trees = qywxDeptService.selectQywxDeptList(corpid);
        return success(trees);
    }

    /**
     * 同步企业微信部门数据，保存到数据库
     *
     * @param corpid
     * @return
     */
    @PreAuthorize("@ss.hasPermi('system:dept:list')")
    @GetMapping("/syncDeptData")
    public AjaxResult syncDeptData(@RequestParam(value = "corpid", required = false) String corpid) {
        String msg = qywxDeptService.syncDeptData(corpid);
        return success(msg);
    }

    @PreAuthorize("@ss.hasPermi('system:dept:list')")
    @GetMapping("/getAllUser/{deptId}")
    public AjaxResult getAllUserByDeptId(@PathVariable(value = "deptId") Integer deptId
            , @RequestParam(value = "corpid", required = false) String corpid) {

        List<QywxDeptService.User> users = qywxDeptService.getAllUserByDeptId(deptId, corpid);
        return success(users);
    }

    @PreAuthorize("@ss.hasPermi('system:dept:list')")
    @GetMapping("/syncUserData/{deptId}")
    public AjaxResult syncUserData(@PathVariable(value = "deptId") Integer deptId
            , @RequestParam(value = "corpid", required = false) String corpid) {
        boolean isSuccess = qywxDeptService.syncUserData(deptId, corpid);
        return success(isSuccess);
    }

}
